<?php
set_time_limit(0);
ob_implicit_flush();

$address = "127.0.0.1";
$port = 8080;
if (($sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) < 0) {
	echo "socket_create() failed: reason: " . socket_strerror($sock) . "\n";
}


if (($ret = socket_bind($sock, $address, $port)) < 0) {
	echo "socket_bind() failed: reason: " . socket_strerror($ret) . "\n";
}

if (($ret = socket_listen($sock, 5)) < 0) {
	echo "socket_listen() failed: reason: " . socket_strerror($ret) . "\n";
}


do {
	if (!($msgsock = socket_accept($sock))) {
		echo "socket_accept() failed: reason: " . socket_strerror($msgsock) . "\n";
		break;
	}

	/* 开始接受socket传过来的数据s. */
	do {
		//如果是安全策略请求,则传输安全策略文件内容
		if($buf = socket_read($msgsock, 2048)){
			if(strpos($buf, 'policy-file-request')){
				$msg ="<cross-domain-policy><allow-access-from domain='*' to-ports='*' /></cross-domain-policy>";
				socket_write($msgsock, $msg."\0", strlen($msg."\0"));
			}

			//答复数据
			$talkback = "you said: '$buf'";
			socket_write($msgsock, $talkback, strlen($talkback));
		}
	} while (true);
	socket_close($msgsock);
} while (true);
socket_close($sock);
?>